package com.gy.controller;

import com.gy.entity.*;
import com.gy.entity.Error;
import com.gy.serivce.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class DataController {

    @Autowired
    private DataService dataService;
    /**
     * 根据用户名和密码进行查询用户
     * @return
     */
    @PostMapping("/login")
    public User login(@RequestParam Map<String, String> params) {
        System.out.println("username" + "==========" + params.get("username"));
        User user = dataService.login(params.get("username"), params.get("password"));
        return user;
    }
    @PostMapping("/reg")
    public void register(@RequestParam Map<String, String> params) {
        dataService.register(params.get("username"), params.get("password"));
    }

    @PostMapping("/search/user")
    public User searchByUsername(@RequestParam Map<String, String> params) {
        return dataService.searchByUsername(params.get("username"));
    }

    /**
     * 获取Java的问题
     * @param params
     * @return
     */
    @PostMapping("/get-java-question")
    public List<Question> getQuestion(@RequestParam Map<String, String> params) {
        return dataService.getQuestion(params.get("type"));
    }

    /**
     * 获取Android的问题
     * @param params
     * @return
     */
    @PostMapping("/get-android-question")
    public List<Question> getAndroidQuestion(@RequestParam Map<String, String> params) {
        return dataService.getAndroidQuestion(params.get("type"));
    }

    @PostMapping("/collect")
    public void collect(@RequestParam Map<String, String> params) {
        dataService.collect(params.get("questionId"), params.get("userId"));
    }
    @PostMapping("/search-collect")
    public Collect searchCollect(@RequestParam Map<String, String> params) {
        return dataService.searchCollect(params.get("questionId"), params.get("userId"));
    }

    /**
     * 查询用户的收藏记录
     * @param params
     * @return
     */
    @PostMapping("/search-collect-user")
    public List<Question> searchCollectByUser(@RequestParam Map<String, String> params) {
        return dataService.searchCollectByUser(params.get("userId"));
    }
    /**
     * 查询用户的错题记录
     * @param params
     * @return
     */
    @PostMapping("/search-error-user")
    public List<Question> searchErrorByUser(@RequestParam Map<String, String> params) {
        return dataService.searchErrorByUser(params.get("userId"));
    }

    /**
     * 根据类型进行查询视频资源
     * @param params
     * @return
     */
    @PostMapping("/search-video")
    public List<Video> searchVideo(@RequestParam Map<String, String> params) {
        return dataService.searchVideo(params.get("type"));
    }

    @PostMapping("/search-error")
    public Error searchError(@RequestParam Map<String, String> params) {
        return dataService.searchError(params.get("questionId"), params.get("userId"));
    }
    @PostMapping("/error")
    public void error(@RequestParam Map<String, String> params) {
        dataService.error(params.get("questionId"), params.get("userId"));
    }

    /**
     * 根据知识ID查详情
     * @param params
     */
    @PostMapping("/search-detail")
    public Detail detail(@RequestParam Map<String, String> params) {
        return dataService.detail(params.get("id"));
    }

    @PostMapping("/search-all-detail")
    public List<Detail> searchAllDetail() {
        return dataService.searchAllDetail();
    }

    @PostMapping("/search-by-title")
    public Detail searchByTitle(@RequestParam Map<String, String> params) {
        return dataService.searchByTitle(params.get("title"));
    }
}
